// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import axios from 'axios'
import router from './router'
import resource from 'vue-resource'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import store from './vuex/store'

require('swiper/dist/css/swiper.css')

Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.prototype.$http = axios
Vue.use(VueAwesomeSwiper)
Vue.use(resource)

// 全局导航钩子
router.beforeEach((to, from, next) => {

  if (to.meta.requireAuth) {

    // console.log(isEmptyObject(store.state.user))
    if(!isEmptyObject(store.state.user)) {
      next();
    }
    else {
      next({
        path: '/login',
                query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
    }
  }
  else {
    next();
  }
})


 function isEmptyObject(obj) {
  for (var key in obj) {
    return false;
  }
  return true;
 }

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: { App }
})
